package com.iyundao.io.mapper;

import com.iyundao.io.model.SysMenu;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by black on 2019/4/22.
 */
@Repository
public interface SysMenuSqlMapper {

    @Select({
            "select sys_menu.menu_url,GROUP_CONCAT(sys_role.sid) as username from sys_menu " ,
            "LEFT JOIN role_menu on role_menu.mid = sys_menu.sid" ,
            "LEFT JOIN sys_role on role_menu.rid = sys_role.sid and sys_role.flag = 1" ,
            "where sys_menu.flag = 1" ,
            "GROUP BY sys_menu.menu_url"
    })
    @Results({
            @Result(column="menu_url", property="menuUrl", jdbcType=JdbcType.VARCHAR),
            @Result(column="username", property="userName", jdbcType=JdbcType.VARCHAR),
    })
    List<Map> selectPermission();

    @Select({
            "<script>"+
            "select sys_menu.* from sys_menu " +
            "INNER JOIN role_menu on sys_menu.sid = role_menu.mid and role_menu.rid = #{rid,jdbcType=CHAR}" +
            "where flag = 1 " +
            "<if test='parentId!=null'>" +
            "AND parent_id = #{parentId}" +
            "</if>" +
            "and menu_type = 1 ORDER BY orde_by"+
            "</script>"
    })
    @Results({
            @Result(column="sid", property="sid", jdbcType=JdbcType.CHAR, id=true),
            @Result(column="real_name", property="realName", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_name", property="menuName", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_url", property="menuUrl", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_type", property="menuType", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent_id", property="parentId", jdbcType=JdbcType.VARCHAR),
            @Result(column="flag", property="flag", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="create_id", property="createId", jdbcType=JdbcType.CHAR),
            @Result(column="orde_by", property="ordeBy", jdbcType=JdbcType.TINYINT)
    })
    List<SysMenu> selectMenuByRoleId(Map record);

    @Select({
            "select sys_menu.* from sys_menu " ,
            "INNER JOIN role_menu on sys_menu.sid = role_menu.mid and role_menu.rid = #{rid,jdbcType=CHAR}" ,
            "where flag = 1 ORDER BY orde_by"
    })
    @Results({
            @Result(column="sid", property="sid", jdbcType=JdbcType.CHAR, id=true),
            @Result(column="real_name", property="realName", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_name", property="menuName", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_url", property="menuUrl", jdbcType=JdbcType.VARCHAR),
            @Result(column="menu_type", property="menuType", jdbcType=JdbcType.VARCHAR),
            @Result(column="flag", property="flag", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_date", property="createDate", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="create_id", property="createId", jdbcType=JdbcType.CHAR),
            @Result(column="orde_by", property="ordeBy", jdbcType=JdbcType.TINYINT)
    })
    List<SysMenu> selectAllMenuByRoleId(String rid);
}
